import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';
interface Message {
	cookie: {
		uk: string;
		[key: string]: string
	};
	urls: {
		base: string;
		origin: string;
		url: string;
	};
	query: {};
	params: {};
	headers: {};
	captcha: string;
	page: {
		page_no: number;
	};
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
// 表名	equ_user_defined_attribute
// 标题	设备自定义属性
// 字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
// _id	id	string	50	ⅹ	√
// productid	产品ID	string	50	√	ⅹ
// equip_no	设备编号	string	32	√	ⅹ
// attr_no	属性编号	string	32	√	ⅹ
// attr_name	属性名称	string	32	√	ⅹ
// attr_value	属性值	string	32	√	ⅹ

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:e:\考核-2\kh\src\device-management-ygh\zj-000004\s001,action_id:' + action_id);

	const q = query(action_id, session_id, systemid);
	q.prepare('equ_user_defined_attribute', ['attr_no', 'attr_name'], { productid }, 5, message.page.page_no, [], []);
	const res = await q.exec();
	const data = res[0];

	const d = data.map((value) => {
		return { ...value, v: JSON.stringify(value) };
	});

	const c = count(action_id, session_id, systemid);
	c.prepare('equ_user_defined_attribute', { productid });
	const [n] = await c.exec();

	log('Service end path:e:\考核-2\kh\src\device-management-ygh\zj-000004\s001,action_id:' + action_id);
	return {
		data: { data: d, n }
	};
}
